package com.hubing.cloud.app.web.model;

import com.hubing.cloud.app.web.constant.SystemConstant;

public class ResponseBean<T> {

    /**
     * 响应编码 (通用成功响应编码为 1)
     */
    private int code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 时间戳（可在验签时，验证时效性）
     */
    private long ts;

    private String requestId;

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    // 构造方法
    private ResponseBean() {
        this.ts = System.currentTimeMillis();
    }

    private ResponseBean(String message) {
        this.code = SystemConstant.STATUS_FAIL;
        this.message = message;
        this.data = null;
        this.ts = System.currentTimeMillis();
    }

    private ResponseBean(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.ts = System.currentTimeMillis();
    }

    private ResponseBean(int code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
        this.ts = System.currentTimeMillis();
    }

    private ResponseBean(int code) {
        this.code = code;
        this.message = null;
        this.data = null;
        this.ts = System.currentTimeMillis();
    }

    private ResponseBean(CodeMsg cm) {
        if (cm == null) {
            return;
        }
        this.code = cm.getCode();
        this.message = cm.getMsg();
        this.data = null;
        this.ts = System.currentTimeMillis();
    }

    // 常用方法
    public static ResponseBean success() {
        return new ResponseBean(SystemConstant.STATUS_OK, SystemConstant.MSG_OK);
    }

    public static <T> ResponseBean<T> success(T data) {
        return new ResponseBean<T>(SystemConstant.STATUS_OK, SystemConstant.MSG_OK, data);
    }

    public static <T> ResponseBean<T> success(String message, T data) {
        return new ResponseBean<T>(SystemConstant.STATUS_OK, message, data);
    }

    public static ResponseBean error() {
        return new ResponseBean(SystemConstant.STATUS_FAIL);
    }

    public static <T> ResponseBean<T> error(String message) {
        return new ResponseBean(message);
    }

    public static <T> ResponseBean<T> error(CodeMsg cm) {
        return new ResponseBean<T>(cm);
    }

    // 属性方法
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public long getTs() {
        return ts;
    }

    public void setTs(long ts) {
        this.ts = ts;
    }

    @Override
    public String toString() {
        return "ResponseBean{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data='" + data + '\'' +
                ", ts=" + ts +
                '}';
    }

}